/*-----------------------------------------------------------------------*/
/* Low level disk I/O module SKELETON for FatFs     (C)ChaN, 2019        */
/*-----------------------------------------------------------------------*/
/* If a working storage control module is available, it should be        */
/* attached to the FatFs via a glue function rather than modifying it.   */
/* This is an example of glue functions to attach various exsisting      */
/* storage control modules to the FatFs module with a defined API.       */
/*-----------------------------------------------------------------------*/

#include "./fatfs/source/ff.h"			/* Obtains integer types */
#include "./fatfs/source/diskio.h"		/* Declarations of disk functions */
#include "./spiflash/spiflash.h"
#include "./sdio/sdio_sdcard.h"

/* Definitions of physical drive number for each drive */
#define DEV_MMC		0	/* Example: Map MMC/SD card to physical drive 1 */
#define DEV_SPI		1	/* Example: Map USB MSD to physical drive 2 */

#define SPI_FLASH_SECTOR_SIZE   512
#define SPI_FLASH_SECTOR_COUNT  (4 * 1024 + 896) * 2/* 前896k给FATFS占用 */
#define SPI_FLASH_BLOCK_SIZE    8               	/* 每个BLOCK有8个扇区 */
#define SPI_FLASH_FATFS_BASE    0					/* FATFS 在外部FLASH的起始地址 从0开始 */

/*-----------------------------------------------------------------------*/
/* Get Drive Status                                                      */
/*-----------------------------------------------------------------------*/

DSTATUS disk_status (
	BYTE pdrv		/* Physical drive nmuber to identify the drive */
)
{
	return FR_OK;
}



/*-----------------------------------------------------------------------*/
/* Inidialize a Drive                                                    */
/*-----------------------------------------------------------------------*/

DSTATUS disk_initialize (
	BYTE pdrv				/* Physical drive nmuber to identify the drive */
)
{
	uint8_t res = 0;

	switch (pdrv) {
	case DEV_MMC :
		res = sd_init();
		break;

	case DEV_SPI :
		res = flash_spi_init();
		break;
	
	default:
		res = 1;
		break;
	}
	
	if (res) {
		return STA_NOINIT;
	} else {
		return FR_OK;
	}
}



/*-----------------------------------------------------------------------*/
/* Read Sector(s)                                                        */
/*-----------------------------------------------------------------------*/

DRESULT disk_read (
	BYTE pdrv,		/* Physical drive nmuber to identify the drive */
	BYTE *buff,		/* Data buffer to store read data */
	LBA_t sector,	/* Start sector in LBA */
	UINT count		/* Number of sectors to read */
)
{
	uint8_t res = 0;
	
	if (!count) return RES_PARERR;   /* count不能等于0，否则返回参数错误 */

	switch (pdrv) {
	case DEV_MMC :
		res = sd_read_disk(buff, sector, count);

		while (res) {     /* 读出错 */
			sd_init();  /* 重新初始化SD卡 */
			res = sd_read_disk(buff, sector, count);
		}

		break;

	case DEV_SPI :
		for (; count > 0; count--) {
			flash_spi_read(buff, SPI_FLASH_FATFS_BASE + sector * SPI_FLASH_SECTOR_SIZE, SPI_FLASH_SECTOR_SIZE);
			sector++;
			buff += SPI_FLASH_SECTOR_SIZE;
		}

		res = 0;
		break;
	
	default:
		res = 1;
	}
	
	if (res == 0x00) {
        return RES_OK;
    } else {
        return RES_ERROR; 
    }
}


/*-----------------------------------------------------------------------*/
/* Write Sector(s)                                                       */
/*-----------------------------------------------------------------------*/

#if FF_FS_READONLY == 0

DRESULT disk_write (
	BYTE pdrv,			/* Physical drive nmuber to identify the drive */
	const BYTE *buff,	/* Data to be written */
	LBA_t sector,		/* Start sector in LBA */
	UINT count			/* Number of sectors to write */
)
{
	uint8_t res = 0;

    if (!count) return RES_PARERR;  /* count不能等于0，否则返回参数错误 */

	switch (pdrv) {
	case DEV_MMC :
		res = sd_write_disk((uint8_t *)buff, sector, count);

		while (res) {		/* 写出错 */
			sd_init();		/* 重新初始化SD卡 */
			res = sd_write_disk((uint8_t *)buff, sector, count);
		}

		break;

	case DEV_SPI :
		for (; count > 0; count--) {
			flash_spi_write((uint8_t *)buff, SPI_FLASH_FATFS_BASE + sector * SPI_FLASH_SECTOR_SIZE, SPI_FLASH_SECTOR_SIZE);
			sector++;
			buff += SPI_FLASH_SECTOR_SIZE;
		}

		res = 0;
		break;

	default:
		res = 1;
	}

	/* 处理返回值，将返回值转成ff.c的返回值 */
    if (res == 0x00) {
        return RES_OK;
    } else {
        return RES_ERROR; 
    }
}

#endif


/*-----------------------------------------------------------------------*/
/* Miscellaneous Functions                                               */
/*-----------------------------------------------------------------------*/

DRESULT disk_ioctl (
	BYTE pdrv,		/* Physical drive nmuber (0..) */
	BYTE cmd,		/* Control code */
	void *buff		/* Buffer to send/receive control data */
)
{
	DRESULT res;
	int result;

	switch (pdrv) {
	case DEV_MMC :
		switch (cmd) {
            case CTRL_SYNC:
                res = RES_OK;
                break;

            case GET_SECTOR_SIZE:
                *(DWORD *)buff = 512;
                res = RES_OK;
                break;

            case GET_BLOCK_SIZE:
                *(WORD *)buff = g_sd_card_info.CardBlockSize;
                res = RES_OK;
                break;

            case GET_SECTOR_COUNT:
                *(DWORD *)buff = g_sd_card_info.CardCapacity / 512;
                res = RES_OK;
                break;

            default:
                res = RES_PARERR;
                break;
        }
		res = (DRESULT)result;
		return res;

	case DEV_SPI :
		switch(cmd){
			case CTRL_SYNC:
				result = RES_OK;
				break;
			case GET_SECTOR_SIZE:
				*(DWORD*)buff = SPI_FLASH_SECTOR_SIZE;
				result = RES_OK;
				break;
			case GET_SECTOR_COUNT:
				*(DWORD*)buff = SPI_FLASH_BLOCK_SIZE;
				result = RES_OK;
				break;
			case GET_BLOCK_SIZE:
				*(DWORD*)buff = SPI_FLASH_SECTOR_COUNT;
				result = RES_OK;
				break;
			default:
				result = RES_PARERR;
				break;
		}
		res = (DRESULT)result;
		return res;
	}

	return RES_PARERR;
}
